package com.xy.utils

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.RequestOptions.bitmapTransform
import com.bumptech.glide.request.target.BitmapImageViewTarget
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition

import com.xy.RotateTransformation
import com.xy.baselibrary.base.App

/**
 * @ClassName GlideUtils
 * @Author xy
 * @Date 2021/8/20 15:03
 * @Description TODO
 */
object GlideUtils {

    lateinit var options: RequestOptions

//    fun showGif(url: Any?, imageView: ImageView) {
//        options = RequestOptions()
//            .error(R.mipmap.kakalib_scan_ray)
//            .placeholder(R.mipmap.kakalib_scan_ray)
//            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
//        Glide.with(App.getInstance()).asGif().load(url)
//            .apply(options).into(imageView)
//    }

//    fun showGifOne(url: Any?, imageView: ImageView) {
//        options = RequestOptions()
//            .error(R.mipmap.kakalib_scan_ray)
//            .placeholder(R.mipmap.kakalib_scan_ray)
//            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
//        Glide.with(App.getInstance()).load(url)
//            .apply(options).into(object : SimpleTarget<Drawable?>() {
//                fun onResourceReady(resource: Drawable, transition: Transition<in Drawable?>?) {
//                    if (resource is GifDrawable) {
//                        val gifDrawable: GifDrawable = resource as GifDrawable
//                        gifDrawable.setLoopCount(1)
//                        imageView.setImageDrawable(gifDrawable)
//                        gifDrawable.start()
//                    }
//                }
//            })
//    }

//    fun showImage(url: Any?, imageView: ImageView?) {
//        options = RequestOptions()
//            .error(R.mipmap.kakalib_scan_ray)
//            .placeholder(R.mipmap.kakalib_scan_ray)
//            .diskCacheStrategy(DiskCacheStrategy.ALL)
//        Glide.with(App.getInstance()).load(url)
//            .apply(options).into(imageView)
//    }

    //设置指定的图片作为默认图
    fun showNormalCenterCropImage(url: Any?, imageView: ImageView, empty: Int) {
        options = RequestOptions()
            .error(empty)
            .placeholder(empty)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
        Glide.with(App.getInstance()).load(url)
            .apply(options).into(imageView)
    }

    //设置指定的图片作为默认图
    fun showNormalImage(url: Any?, imageView: ImageView, empty: Int) {
        options = RequestOptions()
            .error(empty)
            .placeholder(empty)
            .fitCenter()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
        Glide.with(App.getInstance()).load(url)
            .apply(options)
            .into(imageView)
    }

    //设置指定的图片作为默认图
    fun showNormalImageNoPlace(url: Any?, imageView: ImageView, empty: Int) {
        options = RequestOptions()
            .error(empty)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
        Glide.with(App.getInstance()).asBitmap().load(url)
            .apply(options).into(imageView)
    }

    //带角度旋转的
    fun showNormalImageNoCache(url: Any?, imageView: ImageView, empty: Int) {
        options = RequestOptions()
            .error(empty)
            .placeholder(empty)
            .centerCrop()
            .transform(RotateTransformation(0f))
            .diskCacheStrategy(DiskCacheStrategy.NONE)
        Glide.with(App.getInstance()).asBitmap().load(url)
            .apply(options).into(imageView)
    }

    //加载圆形图片
    fun showNormalCircleImage(url: String?, imageView: ImageView, empty: Int) {
        options = RequestOptions()
            .error(empty)
            .placeholder(empty)
            .centerCrop()
        Glide.with(App.getInstance()).asBitmap().load(url)
            .apply(options).into(object : BitmapImageViewTarget(imageView) {
                override fun setResource(resource: Bitmap?) {
                    val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(
                        App.getInstance().getResources(),
                        resource
                    )
                    circularBitmapDrawable.isCircular = true
                    imageView.setImageDrawable(circularBitmapDrawable)
                }
            })
    }


    //根据图片高度动态设置imageview的高度
    fun showNormalHeightImage(url: String?, imageView: ImageView, empty: Int) {
        options = RequestOptions()
            .error(empty)
            .placeholder(empty)
            .centerCrop()
        Glide.with(App.getInstance()).asBitmap().load(url)
            .apply(options)
            .into(object : SimpleTarget<Bitmap?>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
                    val params = imageView.layoutParams
                    params.width = ScreenUtils.getScreenWidth()
                    params.height = resource.height * 100 / resource.width * params.width / 100
                    imageView.layoutParams = params
                    imageView.setImageBitmap(resource)
                    LogUtils.d("宽=" + params.width + "&高=" + params.height)
                    LogUtils.d("宽=" + resource.width + "&高=" + resource.height)
                }
            })
    }

    //毛玻璃效果的图片
//    fun showBlurBg(url: String?, imageView: ImageView?, empty: Int, color: Int) {
//        options =
//            bitmapTransform(MultiTransformation<Bitmap>(BlurTransformation(color), CircleCrop()))
//        Glide.with(App.getInstance()).asBitmap().load(url)
//            .apply(options).into(imageView)
//    }

}